package com.qf.shop.system.modules.promote.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.qf.shop.common.core.utils.R;
import com.qf.shop.system.commnons.SysConfigConstans;
import com.qf.shop.system.modules.sys.service.SysConfigService;
import com.qf.shop.system.utils.PageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import com.qf.shop.system.modules.promote.entity.PromotionEntity;
import com.qf.shop.system.modules.promote.service.PromotionService;


/**
 * 首页推荐
 *
 * @author toString
 * @email toString@qq.com
 * @date 2022-05-13 10:55:20
 */
@RestController
@RequestMapping("/promotion")
public class PromotionController {
    @Autowired
    private PromotionService promotionService;

    @Autowired
    private SysConfigService sysConfigService;

    @GetMapping("/limitList")
    public List<PromotionEntity> limitList() {

        int limit = 3;
        String value = sysConfigService.getValue(SysConfigConstans.Home_PROMOTION_LIMIT);
        if (!ObjectUtils.isEmpty(value)) {
            limit = Integer.parseInt(value);
        }
        return promotionService.limit(limit);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = promotionService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        PromotionEntity promotion = promotionService.getById(id);
        return R.ok().put("promotion", promotion);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PromotionEntity promotion) {
        promotionService.save(promotion);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PromotionEntity promotion) {
        promotionService.updateById(promotion);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        promotionService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
